Software Development
Inheritance and Polymorphism in C++
C++ Inheritance & Polymorphism: Constructors, Destructors, & Inheritance
C++ Inheritance & Polymorphism: Multiple Inheritance & the Diamond Hierarchy
C++ Inheritance & Polymorphism: Pure Virtual Functions & Abstract Classes
C++ Inheritance & Polymorphism: Understanding & Using Polymorphism
C++ Inheritance & Polymorphism: Using Inheritance for is-a Relationships

C++ Inheritance & Polymorphism: Constructors, Destructors, & Inheritance

Course Number:
it_cpipycdj_02_enus
Lesson Objectives

C++ Inheritance & Polymorphism: Constructors, Destructors, & Inheritance

  • discover the key concepts covered in this course
  • compare constructors in base classes and derived classes
  • invoke base class constructors from derived classes
  • access base class fields from the child class
  • recall that constructors are not inherited
  • work with copy constructors in inheritance hierarchies
  • work with destructors in inheritance hierarchies
  • set up an inheritance hierarchy for polymorphism
  • create pointers to derived and base class objects
  • create references to derived and base class objects
  • summarize the key concepts covered in this course

Overview/Description
The order in which constructors and destructors are invoked on an object of a derived class in an inheritance hierarchy is a very important topic. Constructors are invoked in order from the top-most (most base-level) class, down to the most derived class. Destructors are invoked in the reverse order. Explore how constructors work in an inheritance hierarchy, the order in which the base and derived class constructors are invoked, and how initialization lists need to be used in the derived class. Discover how to use copy constructors and destructors in the context of inheritance. Learn how to set up an inheritance hierarchy for polymorphism. Finally, practice using objects in an inheritance hierarchy with variables of pointer and reference types. When you finish this course, you will have a solid foundation in constructors, destructors, and inheritance, setting the stage for runtime polymorphism and dynamic method dispatch.

Target

Prerequisites: none

C++ Inheritance & Polymorphism: Multiple Inheritance & the Diamond Hierarchy

Course Number:
it_cpipycdj_05_enus
Lesson Objectives

C++ Inheritance & Polymorphism: Multiple Inheritance & the Diamond Hierarchy

  • discover the key concepts covered in this course
  • outline the concepts of multiple inheritance and the diamond hierarchy
  • perform multiple inheritance and explore naming collisions
  • use the ‘using’ keyword and implement virtual methods in multiple inheritance
  • demonstrate the diamond hierarchy and virtual public inheritance
  • perform C-style casts and recognize risks associated with them
  • use C++-style static casts and contrast them with C-style casts
  • cast an object of one class to another class
  • perform dynamic casts
  • recognize errors and issues which can result with dynamic casts
  • summarize the key concepts covered in this course

Overview/Description
C++ allows multiple inheritance, which means that one class can have more than one base. C++ also has some powerful mechanisms for dealing with edge cases such as the diamond hierarchy. You will start this course by learning the precise syntax which is required to implement multiple inheritance in C++, specifically, how it’s important to have both base class access specifiers marked independently. You will then move on to the topic of a diamond inheritance hierarchy, in which the grandchild-level derived class has two parents, both of which inherit from the common, grandparent level base class. Finally, you will see how dynamic casts work in the context of an inheritance hierarchy, how these are a lot safer than static casts, and how they return a null value on failure when used with pointers but they throw an exception on failure when used with references.

Target

Prerequisites: none

C++ Inheritance & Polymorphism: Pure Virtual Functions & Abstract Classes

Course Number:
it_cpipycdj_04_enus
Lesson Objectives

C++ Inheritance & Polymorphism: Pure Virtual Functions & Abstract Classes

  • discover the key concepts covered in this course
  • enable runtime polymorphism by marking destructors as virtual
  • identify scenarios in which object slicing can occur
  • recall that default input arguments should not be specified in virtual functions
  • recognize how virtual methods invoked from constructors and destructors are statically dispatched
  • use the scope resolution operator to invoke specific versions of virtual methods
  • define and invoke pure virtual functions
  • outline the concept of abstract classes
  • create pure abstract classes in C++
  • summarize the key concepts covered in this course

Overview/Description
Pure virtual functions and interfaces are very powerful tools in object-oriented programming, and interface-driven programming is a great way of splitting up work across different developers on a large project. You will start this course by considering why your destructors should be marked as virtual in the base class if you have an inheritance hierarchy. You’ll see the harmful effects of not using virtual destructors in an inheritance hierarchy and the problems with object slicing. You will also learn why it is preferable to have variables of reference types to the base class rather than the value types. You will then move to some finer points of working with virtual methods, such as, the inadvisability of using default parameters because these are statically bound. Finally, you will master the syntax and semantics of pure virtual functions and learn how a class that contains even one pure virtual function becomes an abstract class that cannot be instantiated directly.

Target

Prerequisites: none

C++ Inheritance & Polymorphism: Understanding & Using Polymorphism

Course Number:
it_cpipycdj_03_enus
Lesson Objectives

C++ Inheritance & Polymorphism: Understanding & Using Polymorphism

  • discover the key concepts covered in this course
  • outline the concept of name hiding
  • demonstrate ways of getting around name hiding
  • recognize scenarios in which static binding is dangerous
  • contrast compile time and runtime polymorphism
  • explore the mechanics of runtime polymorphism
  • demonstrate how to use the virtual keyword
  • enable runtime polymorphism using the ‘virtual’ keyword
  • demonstrate the use of runtime polymorphism in different scenarios
  • identify the costs associated with runtime polymorphism
  • recall that the signatures of dynamically dispatched methods must be the same
  • demonstrate the covariance edge case in dynamic method dispatch
  • outline the use of the override specification
  • disable overriding and inheritance using the ‘final’ keyword
  • summarize the key concepts covered in this course

Overview/Description
Inheritance forms the basis for polymorphism, specifically runtime polymorphism, which is a powerful object-oriented programming construct. Runtime polymorphism involves a pointer or a reference of the base class type holding an object of the derived class. The beauty of runtime polymorphism is that when virtual methods are invoked on this pointer, the derived class versions are executed. You will start this course by defining name hiding and how most methods on C++ objects are, by default, statically dispatched. You will then learn about polymorphism and dynamic dispatch, which is accomplished using the virtual keyword. Finally, you will learn how the ‘override’ and 'final' specifications can be used to achieve precise semantics and reduce the scope of bugs and typos in your C++ programs.

Target

Prerequisites: none

C++ Inheritance & Polymorphism: Using Inheritance for is-a Relationships

Course Number:
it_cpipycdj_01_enus
Lesson Objectives

C++ Inheritance & Polymorphism: Using Inheritance for is-a Relationships

  • discover the key concepts covered in this course
  • recall how is-a relationships are modeled with inheritance
  • contrast is-a relationships and inheritance with has-a relationships and composition
  • inherit using public inheritance
  • access members of base and derived classes
  • compare public and private members of base classes
  • use the protected access modifier for derived and external classes
  • differentiate the public, private, and protected access modifiers
  • recall how private, public, and protected inheritance works
  • use the base class access specifier
  • use ‘protected’ as the base class access specifier
  • implement private inheritance
  • access fields from parent classes in child classes with the using keyword
  • summarize the key concepts covered in this course

Overview/Description
Inheritance is a powerful, object-oriented language construct in which one class can be related to another class using an is-a relationship. Using inheritance, you can construct complicated class hierarchies in which one class is a parent or a base of another class. Is-a relationships are a great way of performing object-oriented design and are complemented by has-a relationships, which are implemented using composition, rather than inheritance. Explore inheritance relationships by setting up an inheritance hierarchy in C++ and specifying one class as the parent of another class. Then discover how member variables and methods are accessible from the drive class, as well as from external code. Finally, investigate base class access specifiers, and find out how they work with member access specifiers. When you’re finished, you’ll clearly understand how to use inheritance for is-a relationships and how to identify uses for private inheritance.

Target

Prerequisites: none

Close Chat Live